package com.example.baguwen.controller;

import com.example.baguwen.common.PageResult;
import com.example.baguwen.common.Result;
import com.example.baguwen.dto.BaguwenDTO;
import com.example.baguwen.entity.Baguwen;
import com.example.baguwen.service.BaguwenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/baguwen")
public class BaguwenController {
    
    @Autowired
    private BaguwenService baguwenService;
    
    @PostMapping
    public Result<Baguwen> add(@Validated @RequestBody BaguwenDTO baguwenDTO) {
        Baguwen baguwen = baguwenService.add(baguwenDTO);
        return Result.success("添加八股文成功", baguwen);
    }
    
    @PutMapping("/{id}")
    public Result<Baguwen> update(@PathVariable Long id, @Validated @RequestBody BaguwenDTO baguwenDTO) {
        Baguwen baguwen = baguwenService.update(id, baguwenDTO);
        return Result.success("更新八股文成功", baguwen);
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        baguwenService.delete(id);
        return Result.success("删除八股文成功", null);
    }
    
    @GetMapping("/{id}")
    public Result<Baguwen> getById(@PathVariable Long id) {
        Baguwen baguwen = baguwenService.getById(id);
        return Result.success(baguwen);
    }
    
    @GetMapping
    public Result<PageResult<Baguwen>> list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<Baguwen> pageResult = baguwenService.list(pageNum, pageSize);
        return Result.success(pageResult);
    }
    
    @GetMapping("/type/{type}")
    public Result<PageResult<Baguwen>> listByType(
            @PathVariable String type,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<Baguwen> pageResult = baguwenService.listByType(type, pageNum, pageSize);
        return Result.success(pageResult);
    }
    
    @GetMapping("/search")
    public Result<PageResult<Baguwen>> search(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<Baguwen> pageResult = baguwenService.search(keyword, pageNum, pageSize);
        return Result.success(pageResult);
    }
} 